#include "StdAfx.h"
#include "resource.h"
#include "WeatherWebServiceSoap12.nsmap"
#include "WeatherDlg.h"
#include "AboutDlg.h"

#include "misc.h"



LRESULT CWeatherDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	CenterWindow(GetParent());
	SetWindowFont(*this);
	
	// set icons
	HICON hIcon = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON));
	SetIcon(hIcon, TRUE);
	HICON hIconSmall = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON));
	SetIcon(hIconSmall, FALSE);

	// init controls
	_cmbCity = new CComboBox(GetDlgItem(IDC_CMB_CITY));
	_editWeather = new CEdit(GetDlgItem(IDC_TEXT_WEATHER));
	_editCity = new CEdit(GetDlgItem(IDC_TEXT_CITY));
	SetWindowFont(*_cmbCity, 2052);
	SetWindowFont(*_editWeather, 2052);
	SetWindowFont(*_editCity, 2052);
	

	// init soap
	soap_set_imode(&_soap, SOAP_C_UTFSTRING);
	soap_set_omode(&_soap, SOAP_C_UTFSTRING);

	return TRUE;
}

LRESULT CWeatherDlg::OnClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	delete _cmbCity;
	delete _editWeather;
	delete _editCity;

	EndDialog(0);
	return 0;
}


LRESULT CWeatherDlg::OnCbnDropdownCmbCity(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	// TODO: Add your control notification handler code here
	if(_cmbCity->GetCount() != 0)
		return 0;

	::SetCursor(LoadCursorW(NULL, IDC_WAIT));


	_ns1__getSupportCity req;
	_ns1__getSupportCityResponse res;

	req.byProvinceName = new std::wstring(L"");
	int hr = _soap.getSupportCity(&req, &res);
	delete req.byProvinceName;

	if(hr == SOAP_OK)
	{
		auto cities = res.getSupportCityResult->string;
		std::for_each(cities.begin(), cities.end(), [=](std::wstring& s) {

			this->_cmbCity->AddString(s.c_str());

		});
	}
	else
	{
		wchar_t* msg = A2U(_soap.soap_fault_string());
		MessageBox(msg);
		delete[] msg;
	}

	::SetCursor(LoadCursorW(NULL, IDC_ARROW));

	return 0;
}


LRESULT CWeatherDlg::OnCbnSelchangeCmbCity(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	// TODO: Add your control notification handler code here
	int index = _cmbCity->GetCurSel();
	if(index == -1)
		return 0;

	::SetCursor(LoadCursorW(NULL, IDC_WAIT));


	_ns1__getWeatherbyCityName req;
	_ns1__getWeatherbyCityNameResponse res;


	wchar_t* city = new wchar_t[_cmbCity->GetWindowTextLength() + 1];
	_cmbCity->GetWindowText(city, -1);
	std::wstring s(city);
	delete[] city;

	int startIndex = s.find_first_of(L'(');
	int lastIndex = s.find_last_of(L')');
	if(startIndex == -1 || lastIndex == -1)
	{
		MessageBox(L"Invalid city code!");
		goto __exit;
	}

	req.theCityName = new std::wstring(s.c_str() + startIndex + 1, lastIndex - startIndex -1);
	int hr = _soap.getWeatherbyCityName(&req, &res);
	delete req.theCityName;

	if(hr == SOAP_OK)
	{
		auto info = res.getWeatherbyCityNameResult->string;
		auto weather = std::wstring();

		weather = 
			info[0] + L" " +
			info[1] + L"\r\n" +
			info[5] + L" " +
			info[6] + L"\r\n" +
			info[12] + L" " +
			info[13] + L"\r\n" +
			info[17] + L" " +
			info[18] + L"\r\n";

		_editWeather->SetWindowText(weather.c_str());
		_editCity->SetWindowText((info.end() - 1)->c_str());
	}
	else
	{
		wchar_t* msg = A2U(_soap.soap_fault_string());
		MessageBox(msg);
		delete[] msg;
	}

	__exit:
	::SetCursor(LoadCursorW(NULL, IDC_ARROW));

	return 0;
}


LRESULT CWeatherDlg::OnHelpAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	// TODO: Add your command handler code here
	CAboutDlg dlg;
	dlg.DoModal();
	return 0;
}


LRESULT CWeatherDlg::OnCtlColorDlg(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// TODO: Add your message handler code here and/or call default
	return (LRESULT)MaterialStore::Current()->BackgroundBrush();
}


LRESULT CWeatherDlg::OnCtlColorStatic(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// TODO: Add your message handler code here and/or call default
	SetTextColor((HDC)wParam, MaterialStore::Current()->ForegroundColor());
	SetBkMode((HDC)wParam, TRANSPARENT);
	return (LRESULT)MaterialStore::Current()->BackgroundBrush();
}


LRESULT CWeatherDlg::OnCtlColorEdit(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// TODO: Add your message handler code here and/or call default
	SetTextColor((HDC)wParam, MaterialStore::Current()->ForegroundColor());
	SetBkMode((HDC)wParam, TRANSPARENT);
	return (LRESULT)GetStockObject(WHITE_BRUSH);
}
